package com.GlobalPaint.app.Base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.GlobalPaint.app.utils.AppManager;
import com.GlobalPaint.app.utils.CommUtility;

import org.xutils.common.Callback.CommonCallback;
import org.xutils.http.RequestParams;
import org.xutils.x;
/**
 * baseActionBar Activity
 *
 * @author FireAnt（http://my.oschina.net/LittleDY）
 * @created 2014年9月25日 上午11:30:15 引用自：tonlin
 */
public abstract class BaseActivity extends AppCompatActivity {
    public BaseActivity thisActivity;
    public Dialog dlgWaiting;
    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
    }
    public void requestDataGet(final int flag,final Context context,RequestParams params)
    {
        //由于linux服务端不能识别自带的cookie，所以要手动添加cookie
        if(!AppConfig.sCurrentCookie.equals(""))
        {
            params.setUseCookie(false);
            params.addHeader("Cookie",AppConfig.sCurrentCookie);
        }
        x.http().get(params, new CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException arg0) {
                if(dlgWaiting!=null&&dlgWaiting.isShowing())
                {
                    dlgWaiting.hide();
                    CommUtility.ShowMsgLong(context, "数据加载取消");
                }
            }
            @Override
            public void onError(Throwable arg0, boolean arg1) {
                if(dlgWaiting!=null&&dlgWaiting.isShowing())
                {
                    dlgWaiting.hide();
                    CommUtility.ShowMsgLong(context, "数据加载错误"+arg0);
                }
            }
            @Override
            public void onFinished() {
                if(dlgWaiting!=null&&dlgWaiting.isShowing())
                {
                    dlgWaiting.hide();
                }
            }
            @Override
            public void onSuccess(String arg0) {
                dlgWaiting.hide();
                ParseData(flag, arg0);
            }
        });
    }
    public void requestDataPost(final int flag,final Context context,RequestParams params)
    {
        if(!AppConfig.sCurrentCookie.equals(""))
        {
            params.setUseCookie(false);
            params.addHeader("Cookie",AppConfig.sCurrentCookie);
        }
        x.http().post(params, new CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException arg0) {
                if(dlgWaiting!=null&&dlgWaiting.isShowing())
                {
                    dlgWaiting.hide();
                    CommUtility.ShowMsgLong(context, "数据加载取消");
                }
            }
            @Override
            public void onError(Throwable arg0, boolean arg1) {
                if(dlgWaiting!=null&&dlgWaiting.isShowing())
                {
                    dlgWaiting.hide();
                    CommUtility.ShowMsgLong(context, "数据加载错误"+arg0);
                }
            }
            @Override
            public void onFinished() {
                if(dlgWaiting!=null&&dlgWaiting.isShowing())
                {
                    dlgWaiting.hide();
                }
            }
            @Override
            public void onSuccess(String arg0) {
                dlgWaiting.hide();
                ParseData(flag, arg0);
            }
        });
    }
    public void ParseData(int flag,String jsonstr)
    {

    }
}
